package com.study.services;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.study.model.jdao.ICareDAO;
import com.study.model.jdao.ICollectionDAO;
import com.study.model.jdao.ICustomerDAO;
import com.study.model.pojo.Care;
import com.study.model.pojo.Colle;
import com.study.model.pojo.Customer;

import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.study.model.jdao.ICollectionDAO;
import com.study.model.jdao.IDictionaryDAO;
import com.study.model.jdao.ILinkmanDAO;
import com.study.model.jdao.IUserDAO;
import com.study.model.mdao.AnniversaryMapper;
import com.study.model.mdao.CareDAO;
import com.study.model.mdao.ColleDAO;
import com.study.model.mdao.CustomerMapper;
import com.study.model.mdao.LogforcustDAO;
import com.study.model.mdao.UserMDAO;
import com.study.model.pojo.Colle;
import com.study.model.pojo.Dictionary;
import com.study.model.pojo.Linkman;

@Service
@Transactional(rollbackOn = Exception.class)
public class CareService {
	@Autowired
	ICareDAO dao;
	@Autowired
	CareDAO mapper;
	@Autowired
	IDictionaryDAO iDao;
	@Autowired
	ILinkmanDAO linkmandao;
	@Autowired
	ICustomerDAO custdao;
	@Autowired
	IUserDAO userdao;
	@Autowired
	AnniversaryMapper annMapper;
	@Autowired
	IUserDAO iUserDAO;
	@Autowired
	LogforcustDAO logforcustDAO;
	@Autowired
	UserMDAO userMDAO;

	/**
	 * 带条件的查询
	 * 
	 * @param pageNum
	 * @param pageNum
	 * @param input
	 * @param selects
	 * @param cusid 
	 * @return
	 */

	public List<JSONObject> selectBySel(int pageNum, int pageSize, String input, String selects, Integer cusid) {

		PageHelper.startPage(pageNum, pageSize);
		return mapper.selectByWhere(("%" + input + "%"), ("%" + input + "%"), selects, ("%" + input + "%"),cusid);
	}

	/**
	 * 根据id查询单条记录 ty
	 * 
	 * @param parameter
	 * @return
	 */
	public JSONObject selectBycid(String parameter) {
		JSONObject care = null;
		if (!"".equals(parameter)) {
			care = mapper.selectById(parameter);
		}
		return care;
	}

	/**
	 * 添加一条关怀
	 * 
	 * @param theme
	 * @param custname
	 * @param linkman
	 * @param dates
	 * @param user
	 * @param type
	 * @param content
	 * @param back
	 * @param more
	 * @return
	 */
	public boolean addCare(String theme, Integer custid, Integer linkman, Date dates, Integer useid,
			Integer type, String content, String back, String more) {
		Care care = new Care();

		care.setUser(userdao.findByUseId(useid));
		care.setLinId(linkman);
		Customer customer = custdao.findByCusId(custid);
		care.setCustomer(customer);
		care.setCarRemark(more);
		care.setCarName(theme);
		care.setCarFeedback(back);
		care.setCarDate(dates);
		care.setCarContent(content);
		care.setDictionary(iDao.findByDicId(type));
		care.setIsShow(1);
		dao.save(care);

		// 添加一条流水
		String logMessage = "客户关怀&nbsp;&nbsp;" + theme + "====" + customer.getCusName();
		logforcustDAO.insert(custid, new Date(), useid, 230, logMessage, "");
		return true;
	}

	/**
	 * 删除关怀
	 * 
	 * @param goo_id
	 */
	public void delete(int cid) {
		Care care = dao.findById(cid).get();

		// 判断该条服务是否为送礼物，如果为，则只进行逻辑删除；
		if (care.getDictionary().getDicId() == 441) {
			care.setIsShow(0);
		} else {
			dao.delete(care);
		}

	}

	/**
	 * 编辑关怀
	 * 
	 * @param theme
	 * @param custid
	 * @param linId
	 * @param dates
	 * @param useid
	 * @param type
	 * @param content
	 * @param back
	 * @param more
	 * @param id
	 * @return
	 */
	public boolean editorService(String theme, Integer custid, Integer linId, Date dates, Integer useid,
			Integer type, String content, String back, String more, Integer id) {
		Care care = dao.findByCarId(id);
		care.setUser(userdao.findByUseId(useid));
		care.setLinId(linId);
		care.setCustomer(custdao.findByCusId(custid));
		care.setCarRemark(more);
		care.setCarName(theme);
		care.setCarFeedback(back);
		care.setCarDate(dates);
		care.setCarContent(content);
		care.setDictionary(iDao.findByDicId(type));
		return true;
	}

	/**
	 * 搜索是否存在纪念日
	 * @return 
	 */
	public Integer isAnn() {
		return mapper.findCountByDay();

	}

	/**
	 * 查询需要关怀的客户
	 * 
	 * @param id
	 * @return
	 */
	public List<JSONObject> findCareByDay(Integer userid) {
		List<JSONObject> list = userMDAO.selectRoleUserId(userid);
		for (JSONObject jsonObject : list) {
			if (jsonObject.getInteger("rol_id") == 1 || jsonObject.getInteger("rol_id") == 5
					|| jsonObject.getInteger("rol_id") == 9) {
				userid = null;
				break;
			}
		}

		List<JSONObject> list2 = mapper.findCareByDay(userid);
		return list2;
	}

	/**
	 * 修改关怀的car_type+isdo
	 * 
	 * @param type
	 * @param id
	 * @param useid
	 * @param custid 
	 * @param username 
	 * @param theme 
	 * @return
	 */
	public boolean careChange(Integer type, Integer id, Integer useid, Integer custid, String username, String theme) {
		int reslut = mapper.careChange(type, id,useid);
		String meg = null;
		if (type == 441) {
			meg = username + theme +"===送去了礼物。";
		}else if (type == 442) {
			meg = username + theme +"===发短信，送去了问候。";
		}else if (type == 443) {
			meg = username + theme +"===打电话，送去了问候。";
		}
		// 添加一条流水
		logforcustDAO.insert(custid, new Date(), useid, 2202, meg, "");

		return reslut > 0 ? true : false;
	}
}
